"""
员工管理测试脚本
"""
import unittest
from socket import send_fds

from api.api_ihrm_emp_crud import ApiIhrmEmpCrud
from common.get_header import get_header
from config import TEL
from request_case.request_demo1 import headers
from utils.DBUtil import DBUtil


class TestIhrmEmpCrud(unittest.TestCase):
    api_ihrm_emp_crud = None
    db = None  # 类级别的数据库连接对象
    header = None
    def setUpClass(cls):
        # 类夹具
        # 初始化接口对象
        cls.api_ihrm_emp_crud = ApiIhrmEmpCrud()
        # 1.初始化数据库连接
        cls.db = DBUtil(host="211.103.136.244", port=7061, user="student", password="iHRM_student_2021", database="test_db",
                    charset="utf8")
        # 把需要鉴权的header在类夹具中加载一下
        cls.header = get_header()
    def tearDownClass(cls):
        cls.db.close()
    def setUp(self):
        # 解决手机号重复，需要反复修改手机号的问题
        try:
            delete_sql = f"delete from bs_user where mobile = '{TEL}'"
            # 使用工具内置方法，工具内部直接写好了连接，游标，关闭等
            self.db.execute(delete_sql)
        except Exception as e:
            # 出现异常回滚
            print(e)
            self.db.rollback()
    def tearDown(self):
        # 解决手机号重复，需要反复修改手机号的问题
        try:
            delete_sql = f"delete from bs_user where mobile = '{TEL}'"
            # 使用工具内置方法，工具内部直接写好了连接，游标，关闭等
            self.db.execute(delete_sql)
        except Exception as e:
            # 出现异常回滚
            print(e)
            self.db.rollback()
    # 添加-必选参数
    def test01_add_emp(self):
        header =  self.header
        json_data = { "username": "业务猪001", "mobile": "13978734787", "workNumber": "9527" }
        resp = self.api_ihrm_emp_crud.add_emp(header, json_data)
        print("添加-必选参数：", resp.json())
        self.assertEqual(resp.status_code, 200)
        self.assertIn(resp.text, "添加成功")
        self.assertEqual(True, resp.json().get("success"))
        self.assertEqual(10000, resp.json().get("code"))
    # 添加-组合参数
    def test02_add_emp(self):
        # TODO：建立一个只与本项目相关的工具方法用于初始化请求头-Authorization
        header = self.header
        json_data = {"username": "业务猪001", "mobile": "13978743801", "workNumber": "9527", "formOfEmployment": "2"}
        resp = self.api_ihrm_emp_crud.get_emp(header, json_data)
        print("添加-组合参数：", resp.json())
        self.assertEqual(resp.status_code, 200)
        self.assertIn(resp.text, "添加成功")
        self.assertEqual(True, resp.json().get("success"))
        self.assertEqual(10000, resp.json().get("code"))
    # 添加-全部参数
    def test03_add_emp(self):
        header = self.header
        json_data = {"username": "大猪乔治", "mobile": "13899078431", "timeOfEntry": "2021-12-01", "formOfEmployment": 1, "workNumber": "777888", "departmentName": "测试", "departmentId": "1452603344685203456", "correctionTime": "2021-12-30T16:00:00.000Z"}
        resp = self.api_ihrm_emp_crud.add_emp(header, json_data)
        print("添加-全部参数", resp.json())
        self.assertEqual(resp.status_code, 200)
        self.assertIn(resp.text, "添加成功")
        self.assertEqual(True, resp.json().get("success"))
        self.assertEqual(10000, resp.json().get("code"))
